class DayTimeEntity {
  String day;
  List<String> time;
  bool dayIsSelected = false;
  int timeSelected = -1;
  int timeSelectedTemp = -1;
}

List<DayTimeEntity> getDayTime({int days = 2, bool includeToday = true}){
  if(days <= 0) {
    throw Exception('days must > 0');
  }
  List<DayTimeEntity> daysAndTimes = [];
  DateTime today = DateTime.now();
  DateTime deadline = DateTime(today.year, today.month, today.day, 21, 15);
  if(includeToday && today.isBefore(deadline)) {
    // 包括今天且在截止时间之前
    DayTimeEntity todayDayTime = DayTimeEntity();
    todayDayTime.day = '${today.month < 10 ? '0' : ''}${today.month}月${today.day < 10 ? '0' : ''}${today.day}日';
    todayDayTime.time = _getTime(today);
    daysAndTimes.add(todayDayTime);
    days--;
  }
  for(int index = 0; index < days; index++) {
    DateTime nextDay = today.add(Duration(days: index + 1));
    DateTime dateTime = DateTime(nextDay.year, nextDay.month, nextDay.day, 7, 0, 0);
    DayTimeEntity dayTime = DayTimeEntity();
    dayTime.day = '${dateTime.month < 10 ? '0' : ''}${dateTime.month}月${dateTime.day < 10 ? '0' : ''}${dateTime.day}日';
    dayTime.time = _getTime(dateTime);
    daysAndTimes.add(dayTime);
  }
  if(daysAndTimes.length > 0) {
    daysAndTimes[0].time.insert(0, '尽快送达（预计${daysAndTimes[0].time[0].split('-')[1]}前）');
    daysAndTimes[0].timeSelected = 0;
    daysAndTimes[0].timeSelectedTemp = 0;
//    daysAndTimes[0].dayIsSelected = true;
  }
  return daysAndTimes;
}

List<String> _getTime(DateTime startTime) {
  List<String> timePeriods = [];
  if (startTime.hour < 7) {
    startTime = DateTime(startTime.year, startTime.month, startTime.day, 7, 0, 0, 0);
  }
  DateTime start;
  DateTime end;
  if(startTime.minute == 0) {
    start = startTime;
    end = start.add(Duration(minutes: 30));
  } else if (startTime.minute > 0 && startTime.minute <= 15) {
    start = startTime.add(Duration(minutes: 30 - startTime.minute));
    end = start.add(Duration(minutes: 30));
  } else if (startTime.minute > 15 && startTime.minute <= 30) {
    start = startTime.add(Duration(minutes: 45 - startTime.minute));
    end = start.add(Duration(minutes: 30));
  } else if (startTime.minute > 30 && startTime.minute <= 45) {
    start = startTime.add(Duration(minutes: 60 - startTime.minute));
    end = start.add(Duration(minutes: 30));
  } else {
    start = startTime.add(Duration(minutes: 60 - startTime.minute + 15));
    end = start.add(Duration(minutes: 30));
  }
  if (end.hour < 22 || (end.hour == 22 && end.minute == 0)) {
    do {
      timePeriods.add(
          '${start.hour < 10 ? '0' : ''}${start.hour}:${start.minute}${start.minute == 0 ? '0' : ''}'
              '-${end.hour < 10 ? '0' : ''}${end.hour}:${end.minute}${end.minute == 0 ? '0' : ''}');
      start = end;
      end = start.add(Duration(minutes: 30));
    } while (end.hour < 22 || (end.hour == 22 && end.minute == 0));
  }
  return timePeriods;
}